package com.ruoyi.course.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.course.domain.vo.ChapterVo;
import com.ruoyi.system.domain.TTextContent;
import com.ruoyi.system.mapper.TTextContentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.TChapterMapper;
import com.ruoyi.course.domain.TChapter;
import com.ruoyi.course.service.ITChapterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 章节Service业务层处理
 *
 * @author Bandit
 * @date 2022-11-01
 */
@Service
public class TChapterServiceImpl implements ITChapterService
{
    @Autowired
    private TChapterMapper tChapterMapper;

    @Autowired
    private TTextContentMapper tTextContentMapper;

    /**
     * 查询章节
     *
     * @param id 章节主键
     * @return 章节
     */
    @Override
    public TChapter selectTChapterById(Long id)
    {
        return tChapterMapper.selectTChapterById(id);
    }

    @Override
    public ChapterVo selectChapterVoById(Long id) {
        return tChapterMapper.selectChapterVoById(id);
    }

    /**
     * 查询章节列表
     *
     * @param tChapter 章节
     * @return 章节
     */
    @Override
    public List<TChapter> selectTChapterList(TChapter tChapter)
    {
        return tChapterMapper.selectTChapterList(tChapter);
    }

    @Override
    public List<ChapterVo> selectChapterVoList(ChapterVo chapterVo) {
        return tChapterMapper.selectChapterVoList(chapterVo);
    }

    @Override
    public Long[] selectIdsByCourseId(Long courseId) {
        return tChapterMapper.selectIdsByCourseId(courseId);
    }

    /**
     * 新增章节
     *
     * @param tChapter 章节
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTChapter(ChapterVo chapterVo)
    {
        // 拷贝到TChapter
        TChapter tChapter = new TChapter();
        BeanUtils.copyProperties(chapterVo, tChapter);
        // 插入内容
        TTextContent textContent = new TTextContent();
        textContent.setContent(chapterVo.getContent());
        textContent.setCreateTime(new Date());
        tTextContentMapper.insertTTextContent(textContent);
        // 拿到contentId
        tChapter.setContentId(textContent.getId());
        TChapter parent = tChapterMapper.selectTChapterById(tChapter.getParentId());
        if (parent == null) {
            tChapter.setAncestors("0");
        } else {
            tChapter.setAncestors(parent.getAncestors() + "," + parent.getId());
        }
        return tChapterMapper.insertTChapter(tChapter);
    }

    /**
     * 修改章节
     *
     * @param tChapter 章节
     * @return 结果
     */
    @Override
    public int updateTChapter(TChapter tChapter)
    {
        return tChapterMapper.updateTChapter(tChapter);
    }

    /**
     * 批量删除章节
     *
     * @param ids 需要删除的章节主键
     * @return 结果
     */
    @Override
    public int deleteTChapterByIds(Long[] ids)
    {
        return tChapterMapper.deleteTChapterByIds(ids);
    }

    /**
     * 删除章节信息
     *
     * @param id 章节主键
     * @return 结果
     */
    @Override
    public int deleteTChapterById(Long id)
    {
        return tChapterMapper.deleteTChapterById(id);
    }

    @Override
    @Transactional
    public int updateChapterVo(ChapterVo chapterVo) {
        TChapter tChapter = new TChapter();
        BeanUtils.copyProperties(chapterVo, tChapter);
        // 更新内容如果有修改
        TTextContent textContent = new TTextContent();
        Long contentId = chapterVo.getContentId();
        if (contentId != null) {
            textContent.setId(contentId);
        }
        textContent.setContent(chapterVo.getContent());
        tTextContentMapper.updateTTextContent(textContent);
        // 更新章节信息
        return tChapterMapper.updateTChapter(tChapter);
    }
}
